package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TransferAccountsPeopleAddDTO;
import org.jsola.hr.dto.TransferAccountsPeopleH5AddDTO;
import org.jsola.hr.dto.TransferAccountsPeopleUpdateDTO;
import org.jsola.hr.query.TransferAccountsPeopleQuery;
import org.jsola.hr.service.ITransferAccountsPeopleService;
import org.jsola.hr.vo.TransferAccountsPeopleListVO;
import org.jsola.hr.vo.TransferAccountsPeoplePasteVO;
import org.jsola.hr.vo.TransferAccountsPeopleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 转账人员库
 *
 * @author zhr
 */
@RestController("hrTransferAccountsPeopleController")
@RequestMapping(HrConstants.API_PATH + "/v1/transferAccountsPeoples")
public class TransferAccountsPeopleController implements ITransferAccountsPeopleControllerAPI {

    @Autowired
    private ITransferAccountsPeopleService transferAccountsPeopleService;

    @Override
    @PostMapping
    public Result<TransferAccountsPeopleVO> save(@Valid @RequestBody TransferAccountsPeopleAddDTO transferAccountsPeopleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增转账人员库权限校验

        return Result.success(
                transferAccountsPeopleService.save(transferAccountsPeopleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TransferAccountsPeopleUpdateDTO transferAccountsPeopleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改转账人员库权限校验

        int count = transferAccountsPeopleService.update(transferAccountsPeopleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{transferAccountsPeopleId}")
    public Result<Integer> delete(@PathVariable String transferAccountsPeopleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除转账人员库权限校验

        int count = transferAccountsPeopleService.deleteByIds(tokenUser, transferAccountsPeopleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] transferAccountsPeopleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除转账人员库权限校验

        int count = transferAccountsPeopleService.deleteByIds(tokenUser, transferAccountsPeopleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{transferAccountsPeopleId}")
    public Result<TransferAccountsPeopleVO> findById(@PathVariable String transferAccountsPeopleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账人员库权限校验

        return Result.success(
            transferAccountsPeopleService.selectById(transferAccountsPeopleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TransferAccountsPeopleListVO>> listAll(@Valid TransferAccountsPeopleQuery transferAccountsPeopleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账人员库权限校验

        return Result.success(
            transferAccountsPeopleService.select(transferAccountsPeopleQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TransferAccountsPeopleListVO>> page(@Valid TransferAccountsPeopleQuery transferAccountsPeopleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            transferAccountsPeopleService.selectPage(transferAccountsPeopleQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("h5SaveData")
    public Result<Integer> h5SaveData(@RequestBody TransferAccountsPeopleH5AddDTO transferAccountsPeopleH5AddDTO) {
        return Result.success(
                transferAccountsPeopleService.h5SaveData(transferAccountsPeopleH5AddDTO)
        );
    }

    @Override
    @PostMapping("/exportTransferAccountsPeopleList")
    public ResponseEntity<Resource> exportTransferAccountsPeopleList(@RequestBody TransferAccountsPeopleQuery transferAccountsPeopleQuery) {
        TokenUser tokenUser = UserContext.getUser();

        // 导出excel
        String excelFilePath = transferAccountsPeopleService.exportTransferAccountsPeopleList(transferAccountsPeopleQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = FileKit.getNameAndEncode(excelFilePath);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @PostMapping("/selectByIdCards")
    public Result<TransferAccountsPeoplePasteVO> selectByIdCards(@RequestBody TransferAccountsPeopleQuery transferAccountsPeopleQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountsPeopleService.selectByIdCards(transferAccountsPeopleQuery, tokenUser)
        );
    }


}

